package br.com.duduso.conferencia_claro_desktop.dbf;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import jxl.Sheet;
import jxl.Workbook;

/**
 *
 * @author duduso
 */
public class ConexaoDBF {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {

            gerarDBFPadrao(null, null, null);

    }

    public static void gerarDBFPadrao(File xlsClientesAtivos, File dbfDatabase, String arquivoSaida) throws Exception{

            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

            List<String> listaCnpjClientesAtivos = new ArrayList<String>();
            List<String> listaCnpjAdicionados = new ArrayList<String>();

            Workbook wb = Workbook.getWorkbook(xlsClientesAtivos);
            Sheet sheet = wb.getSheet(0);
            int linhas = sheet.getRows();

            for(int i = 0; i < linhas; i++){
                    String value = sheet.getCell(0, i).getContents();
                    if(value != null && value.length() > 0 && i > 0){
                            String newValue = value.replaceAll("\\.", "").replaceAll("/", "").replaceAll("-", "");
                            listaCnpjClientesAtivos.add(newValue);
                    }
            }

            wb.close();

            InputStream stream = new FileInputStream(dbfDatabase.getPath());
            DBFReader reader = new DBFReader(stream);
            List<Object[]> rows = new ArrayList<Object[]>();
            Object[] row;

            while((row = reader.nextRecord()) != null){
                    String cnpj = ((String) row[0]).replace("\\.", "").replace("-", "").replace("/", "").replace(":", "");
                    if(cnpj != null
                            && !listaCnpjAdicionados.contains(cnpj)
                            && listaCnpjClientesAtivos.contains(cnpj)){
                            row[59] = sdf.parse(sdf.format(new Date()));
                            rows.add(row);
                            listaCnpjAdicionados.add(cnpj);
                    }
            }

            DBFUtils.gerarArquivoDBF(DBFUtils.getFields(reader), rows, arquivoSaida);
            stream.close();

    }

    private static void compararDBF() throws Exception{

            InputStream stream01 = new FileInputStream("/home/duduso/Downloads/CLIENTE_BIKE.dbf");
            InputStream stream02 = new FileInputStream("/home/duduso/Downloads/CLIENTE.DBF");
            DBFReader reader01 = new DBFReader(stream01);
            DBFReader reader02 = new DBFReader(stream02);
            for(int i = 0; i < reader01.getFieldCount(); i++){
                    DBFField field01 = reader01.getField(i);
                    DBFField field02 = reader02.getField(i);
            }
            Object[] value;
            while((value = reader01.nextRecord()) != null){
                    System.out.println(value[1] + " - " + value[59]);
            }
            stream01.close();
            stream02.close();

    }

    private static void gerarArquivoTXT(List<Object[]> rows) throws Exception{
            StringBuilder arquivo = new StringBuilder();
            arquivo.append(ArquivoUtils.getCabecalhoArquivo(1, "18.715.391/0001-96", "FILIAL BETIM",
                    1, "TESTE DE GERAÇÃO DE ARQUIVOS", "PERFIL TESTE", "0", 72, new Date(), "duduso"));
            for(Object[] o : rows){
                    arquivo.append(ArquivoUtils.getConteudoArquivo(
                            (rows.indexOf(o) + 1),
                            String.valueOf(o[0]), 
                            null,
                            "15",
                            String.valueOf(o[1]),
                            "teste@yahoo.com.br"));
            }
            PrintWriter pw = new PrintWriter(new File("/home/duduso/Teste.txt"));
            pw.print(arquivo.toString());
            pw.close();
    }

}
